package com.zhq.blog.manage.domain.fronted.controller;

import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhq.blog.core.domain.user.User;
import com.zhq.blog.manage.domain.fronted.controller.support.RandomValidateCode;
import com.zhq.blog.manage.domain.fronted.controller.support.UserInterceptor;

/**
 * @title:LoginController.java
 * @package com.zhq.blog.manage.domain.fronted.controller
 * @author 张恒强
 * @date 2015年1月5日 下午2:24:20
 * @version V1.0
 */
@Controller
public class LoginController extends BaseController{
	@RequestMapping("/login")
	public MallModelView login(HttpServletRequest request){
		return buildModelView("/login", request);
	}
	@RequestMapping("/login/valid")
	public MallModelView valid(String idOrEmail, String password,
			String remember,String checkcode, HttpServletRequest request,
			HttpServletResponse response){
		if(!checkcode.toUpperCase().equals(request.getSession().getAttribute(RandomValidateCode.RANDOMCODEKEY))){
			return buildModelView("/login", request).add("statMessage","验证码错误！！");
		}
		Pattern checkEmail = Pattern
				.compile("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\\.([a-zA-Z0-9_-])+)+$");
		List<User> user = null;
		if (checkEmail.matcher(idOrEmail).matches()) {//邮箱
			user = context.getUserRepository().find_by_email_password(idOrEmail,password);
		}else{//ID
			user = context.getUserRepository().find_by_id_password(Long.parseLong(idOrEmail),password);
		}
		if(CollectionUtils.isNotEmpty(user)){
			if (user.get(0).isFlag()) {
				return buildModelView("/login", request).add("statMessage","用户已冻结，请联系客服！");
			}else{
				if(remember!=null && remember.equals("1")){
					request.setAttribute("redirect_remember", "1");
					request.setAttribute("redirect_idorEmail", idOrEmail);
					request.setAttribute("redirect_id",user.get(0).getId());
				}
				request.getSession().setAttribute(UserInterceptor.SESSION_ID_FLAG, user.get(0));
				return buildModelView("redirect:/index",request);
			}
		}else{
			return buildModelView("/login", request).add("statMessage","用户名或者密码错误");
		}
	}
	
	@RequestMapping("/form_login")
	public @ResponseBody()  String form_login(String idOrEmail, String password,
			String remember,String checkcode, HttpServletRequest request,
			HttpServletResponse response){
		if(!checkcode.toUpperCase().equals(request.getSession().getAttribute(RandomValidateCode.RANDOMCODEKEY))){
			return "1";
		}
		Pattern checkEmail = Pattern
				.compile("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\\.([a-zA-Z0-9_-])+)+$");
		List<User> user = null;
		if (checkEmail.matcher(idOrEmail).matches()) {//邮箱
			user = context.getUserRepository().find_by_email_password(idOrEmail,password);
		}else{//ID
			user = context.getUserRepository().find_by_id_password(Long.parseLong(idOrEmail),password);
		}
		if(CollectionUtils.isNotEmpty(user)){
			if (user.get(0).isFlag()) {
				return "2";
			}else{
				request.getSession().setAttribute(UserInterceptor.SESSION_ID_FLAG, user.get(0));
				return "true";
			}
		}else{
			return "3";
		}
	}
	
	@RequestMapping("/logout")
	public MallModelView logout(HttpServletRequest request){
		
		request.getSession().removeAttribute(UserInterceptor.SESSION_ID_FLAG);
		return buildModelView("redirect:/index", request);
	}
	
	@RequestMapping("/verificationCode")
	public  void verificationCode(HttpServletRequest request,HttpServletResponse response){
		new RandomValidateCode().getRandcode(request, response);
	}
}
